package com.foreknow.config;


import com.foreknow.interceptor.TokenInterceptor;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.concurrent.ConcurrentTaskExecutor;
import org.springframework.web.servlet.config.annotation.AsyncSupportConfigurer;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Executors;

/**
 * 跨域请求支持/token拦截
 * tip:只能写在一个配置类里
 */
@Configuration
public class WebConfiguration implements WebMvcConfigurer {

    private TokenInterceptor tokenInterceptor;

    //构造方法
    public WebConfiguration(TokenInterceptor tokenInterceptor){
        this.tokenInterceptor = tokenInterceptor;
    }

    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowCredentials(true)
                .allowedHeaders("*")
                .allowedMethods("*")
                .allowedOrigins("*");
    }

    @Override
    public void configureAsyncSupport(AsyncSupportConfigurer configurer){
        configurer.setTaskExecutor(new ConcurrentTaskExecutor(Executors.newFixedThreadPool(3)));
        configurer.setDefaultTimeout(30000);
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry){
        List<String> excludePath = new ArrayList<>();
        //排除拦截
        excludePath.add("/user/register");  //登录
        excludePath.add("/user/login");     //注册
        excludePath.add("/user/findUserByPage");//分页
        excludePath.add("/user/queryUser");//查询所有测试
        excludePath.add("/user/insertUser");//添加用户信息测试
        excludePath.add("/user/deleteUserById");//删除用户信息测试
        excludePath.add("/user/deleteByChoose");//批量删除用户测试
        excludePath.add("/department/queryDeptName");//查询所有医生职称测试
        excludePath.add("/user/updateUser");//修改用户信息测试
        excludePath.add("/user/selectUserById");//根据id查询用户信息测试

        excludePath.add("/department/deleteDepById");//删除科室信息测试
        excludePath.add("/department/insertDep");//删除科室信息测试
        excludePath.add("/department/deleteByChoose");//批量删除测试科室
        excludePath.add("/department/updateUser");//批量删除测试科室
        excludePath.add("/department/queryDep");//批量删除测试科室
        excludePath.add("/department/selectUserById");//根据id查询科室信息

        excludePath.add("/registlevel/queryAll");//查询所有挂号等级测试
        excludePath.add("/registlevel/insertReg");//查询所有挂号等级测试deleteById
        excludePath.add("/registlevel/deleteById");//查询所有挂号等级测试
        excludePath.add("/registlevel/deleteByChoose");//查询所有挂号等级测试
        excludePath.add("/registlevel/selectUserById");//根据id查询挂号等级级别测试
        excludePath.add("/registlevel/updateReg");//根据id查询挂号等级级别测试queryReg
        excludePath.add("/registlevel/queryReg");//根据id查询挂号等级级别测试

        excludePath.add("/constantitem/queryAll");//查询所有常数测试
        excludePath.add("/constantitem/selectConById");//根据id查询常数测试queryCon
        excludePath.add("/constantitem/queryCon");//根据条件查询常数测试
        excludePath.add("/constantitem/deleteConById");//根据id常数删除测试
        excludePath.add("/constantitem/deleteByChoose");//批量删除测试
        excludePath.add("/constantitem/insertCon");//添加常数测试
        excludePath.add("/constantitem/queryCon");//条件查询常数
        excludePath.add("/constantitem/updateUser");//根据条件修改

        //结算
        excludePath.add("/settlecategory/findSettleByPage");
        excludePath.add("/settlecategory/deleteSettleById");
        excludePath.add("/settlecategory/deleteSettlesById");
        excludePath.add("/settlecategory/insertSettle");
        excludePath.add("/settlecategory/findSettleById");
        excludePath.add("/settlecategory/updateSettle");


        excludePath.add("/constanttype/queryConstantName");//查询常数 类型  测试

        excludePath.add("/rule/queryByPage");//查询分页

        excludePath.add("/scheduling/findSchedulingByPage");//新增排班规则查询分页

//疾病管理模块
        // 疾病信息
        excludePath.add("/disease/updateDisease");   //修改疾病信息
        excludePath.add("/disease/addDisease");   //添加疾病信息
        excludePath.add("/disease/deleteDisease");   //删除疾病信息
        excludePath.add("/disease/selectDiseaseOne");   //查询一条疾病信息
        excludePath.add("/disease/findAllDiseaseByPage");   //查询所有疾病信息
        // 疾病分类信息
        excludePath.add("/disease/findDiseaseByPage"); //分页查询疾病信息管理
        excludePath.add("/disease/findDisecategoryByPage"); //分页查询疾病分类信息管理
        excludePath.add("/disease/addCategory"); //添加疾病分类信息
        excludePath.add("/disease/deleteCategory"); //删除疾病分类信息
        excludePath.add("/disease/selectCategoryOne");  //查询一个疾病分类信息
        excludePath.add("/disease/updateCategory");  //修改疾病分类信息


        excludePath.add("/static/**");  //静态资源
        excludePath.add("/assets/**");  //静态资源

        registry.addInterceptor(tokenInterceptor)
                .addPathPatterns("/**")
                .excludePathPatterns(excludePath);
        WebMvcConfigurer.super.addInterceptors(registry);
    }
}